package com.randi.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.randi.common.utils.SecurityUtils;
import com.randi.system.domain.entity.sysLog.SysLogLogin;
import com.randi.system.domain.entity.sysLog.SysLogOperate;
import com.randi.system.domain.params.sysLog.SysLogLoginParam;
import com.randi.system.domain.params.sysLog.SysLogOperateParam;
import com.randi.system.domain.vo.IndexInfo;
import com.randi.system.service.ISysIndexService;
import com.randi.system.service.sysLog.ISysLogLoginService;
import com.randi.system.service.sysLog.ISysLogOperateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysIndexServiceImpl implements ISysIndexService {

    @Resource
    private ISysLogLoginService iSysLogLoginService;

    @Resource
    private ISysLogOperateService iSysLogOperateService;

    private void initPage() {
        PageHelper.startPage(1, 6);
    }

    /**
     * 设置最近登录日志
     *
     * @param indexInfo 首页信息
     */
    private void setLastLogLogin(IndexInfo indexInfo) {
        initPage();
        SysLogLoginParam param = new SysLogLoginParam();
        if (!SecurityUtils.isAdmin()) {
            param.setUserName(SecurityUtils.getUsername());
        }
        List<SysLogLogin> sysLogLogins = iSysLogLoginService.selectPageList(param).getList();
        indexInfo.setLastLogLogin(sysLogLogins);
    }

    /**
     * 设置最近操作日志
     *
     * @param indexInfo 首页信息
     */
    private void setLastLogOperate(IndexInfo indexInfo) {
        initPage();
        SysLogOperateParam param = new SysLogOperateParam();
        if (!SecurityUtils.isAdmin()) {
            param.setCreateBy(SecurityUtils.getUserId());
        }
        PageInfo<SysLogOperate> sysLogOperates = iSysLogOperateService.selectPageList(param);
        indexInfo.setLastLogOperate(sysLogOperates.getList());
    }

    @Override
    public IndexInfo getInfo() {
        IndexInfo indexInfo = new IndexInfo();
        setLastLogLogin(indexInfo);
        setLastLogOperate(indexInfo);
        return indexInfo;
    }
}
